package com.yuanqi.dz.dc.adapters;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import pnbclient.helper.StringHelper;
import pnbclient.util.PnbLogFactory;
import pnblog.PnbLogging;

import com.yuanqitech.powermop.dz.DZInfo;
import com.yuanqitech.powermop.dz.DZInfoArray;

public class YinShiReadFile {
	
	public static final PnbLogging LOG = PnbLogFactory.getLogger("DZ_EXT_LOG");
	
	private String charset = "utf-8";
	private String start_row_s ="1";
	private int ReferNumIndex = 0;
	private int TranTimeIndex = 4;
	private int BatchNumIndex =1;
	private int TraceNumIndex = 2;
	private int tranTypeIndex = 3;
	private int TranCardNumIndex = 5;
	private int TermIdIndex = 6;
	private int AmountIndex = 7;
	private int PayFlowNOIndex = 8;

	public DZInfoArray ReadFileYinshi(File file,String  mercharnum,String dzDate){
		DZInfoArray d = new DZInfoArray();

		if(file.exists()){
			BufferedReader reader=null;
			List<DZInfo> list=new ArrayList<DZInfo>();
			try {
				reader =pnbclient.helper.StringHelper.isEmpty(charset) ? new BufferedReader(new FileReader(file)): new BufferedReader(new InputStreamReader(new FileInputStream(file), Charset.forName(charset)));
				String lineText = null;
				int  start_row=StringHelper.isEmpty(start_row_s)?1:Integer.valueOf(start_row_s);
				int  n=1;
				DZInfo  dzInfo = null; 
				while ((lineText = reader.readLine()) != null ) {
					if(n>=start_row && !StringHelper.isEmpty(lineText)){
						dzInfo = new DZInfo();
						String[] ss = lineText.split(",");
						String refernum = ss[ReferNumIndex];
						String BatchNum = ss[BatchNumIndex];
						String TraceNum = ss[TraceNumIndex];
						String Tran_type = ss[tranTypeIndex];
						String trantime = ss[TranTimeIndex];
						String TranCardNum = ss[TranCardNumIndex];
						String TermId = ss[TermIdIndex];
						String Amount = ss[AmountIndex];
						String PayFlowNO = ss[PayFlowNOIndex];
						String refernum_new = null;
						if("S1".equals(Tran_type)){
							refernum_new = refernum;
						}else if("S3".equals(Tran_type)){
							refernum_new = refernum + trantime.replaceAll("-", "").replaceAll(":", "").replaceAll("\\s*", "");
						}
						dzInfo.setReferNum(refernum_new);
						dzInfo.setAmount(Float.valueOf(Amount));
						dzInfo.setTranCode(Tran_type);
						dzInfo.setTrDateTime(trantime);
						dzInfo.setTermId(TermId);
						dzInfo.setPayFlowNO(PayFlowNO);
						dzInfo.setTranCardNum(TranCardNum);
						dzInfo.setExt1(TraceNum); //  终端流水号
						dzInfo.setExt2(BatchNum); //  批次号
						dzInfo.setExt3(refernum);
						list.add(dzInfo);
					}
				}				
				if(null!= list && list.size() > 0){
					DZInfo[] dzs = list.toArray(new DZInfo[list.size()]);
					d.setRc("1");
					d.setDzInfo(dzs);
				}else{
					d.setRc("-1");
					d.setDzInfo(new DZInfo[0]);
					d.setErrormsg("获取对账数据为空");
				}
				return d;	
			}catch (Exception e) {
				d.setRc("-1");
				d.setDzInfo(new DZInfo[0]);
				d.setErrormsg("获取对账数据异常");
			}
		}
		return d;
	}
	
	
	public List<DZInfo> getListReadFileYinshi(File file,String  mercharnum,String dzDate){

		List<DZInfo> list=new ArrayList<DZInfo>();
		if(file.exists()){
			
			BufferedReader reader=null;
			try {
				reader =pnbclient.helper.StringHelper.isEmpty(charset) ? new BufferedReader(new FileReader(file)): new BufferedReader(new InputStreamReader(new FileInputStream(file), Charset.forName(charset)));
				String lineText = null;
				int  start_row=StringHelper.isEmpty(start_row_s)?1:Integer.valueOf(start_row_s);
				int  n=1;
				DZInfo  dzInfo = null; 
				while ((lineText = reader.readLine()) != null ) {
					
					if(n>=start_row && !StringHelper.isEmpty(lineText)){
						
						dzInfo = new DZInfo();
						String[] ss = lineText.split(",");
						String refernum = ss[ReferNumIndex];
						String BatchNum = ss[BatchNumIndex];
						String TraceNum = ss[TraceNumIndex];
						String Tran_type = ss[tranTypeIndex];
						String trantime = ss[TranTimeIndex];
						String TranCardNum = ss[TranCardNumIndex];
						String TermId = ss[TermIdIndex];
						String Amount = ss[AmountIndex];
						String PayFlowNO = ss[PayFlowNOIndex];
						String refernum_new = null;
						if("S1".equals(Tran_type)){
							refernum_new = refernum;
						}else if("S3".equals(Tran_type)){
							refernum_new = refernum + trantime.replaceAll("-", "").replaceAll(":", "").replaceAll("\\s*", "");
						}
						dzInfo.setReferNum(refernum_new);         //检索参考号
						dzInfo.setAmount(Float.valueOf(Amount));  //金额
						dzInfo.setTranCode(Tran_type);            //交易类型          
						dzInfo.setTrDateTime(trantime);           //交易时间
						dzInfo.setTermId(TermId);                 //终端号
						dzInfo.setPayFlowNO(PayFlowNO);           //平台流水号     
						dzInfo.setTranCardNum(TranCardNum);       //卡号
						dzInfo.setExt1(TraceNum);                 //终端流水号
						dzInfo.setExt2(BatchNum);                 //批次号
						dzInfo.setExt3(refernum);                 //原检索参考号
					//	if(dzDate.equals(trantime.replaceAll("-", "").substring(0, 8))){
							list.add(dzInfo);
						//}
						n++;
							
					}
				}				
				
				return list;	
			}catch (Exception e) {
				LOG.addError("解析银石对账文件异常:"+e);
			}
		}
		return list;
	}
	
}
